﻿/// <reference path="_namespace.js" />

/*****************************************************************************
Ajaxna library class definition. Inheriting from ajax.xna.framework.Game.
Author: Kevin 8/16/2009 11:57:23 PM.
Class:  cubeSample.Game1
*****************************************************************************/

$addNamespace("cubeSample");
$imports("ajax.xna.framework.GraphicsDeviceManager");
$imports("ajax.geometry.Mesh");

//-----------------------------------------------------------------------------

cubeSample.Game1 = function(doc)
{///<summary>Creates a new instance of the cubeSample.Game1 game class.</summary>
    cubeSample.Game1.superConstructor.call(this, doc);

    this.graphics = new ajax.xna.framework.GraphicsDeviceManager(this);
    this.graphics.preferredBackBufferWidth = 1024;
    this.graphics.preferredBackBufferHeight = 768;  
}
//-----------------------------------------------------------------------------
$extend("cubeSample.Game1", "ajax.xna.framework.Game");    // Inherits from ajax.xna.Game
if ($getDesignMode()) cubeSample.Game1.prototype = ajax.xna.framework.Game.prototype; // Enable local intellisense.
//-----------------------------------------------------------------------------


cubeSample.Game1.prototype._mesh = ajax.geometry.Mesh.prototype;

// Public members
//-----------------------------------------------------------------------------
cubeSample.Game1.prototype.graphics = ajax.xna.framework.GraphicsDeviceManager.prototype;


// Public Methods & overrides 
//-----------------------------------------------------------------------------
cubeSample.Game1.prototype.initialize = function()
{/// <summary>Allows the game to perform any initialization it needs to before starting to run.</summary>
    
    // TODO: Add your one time initialization code here 
    this._mesh = ajax.geometry.Mesh.createBox(this.getDocument(), 1, 1, 1, ajax.drawing.Colour.green());
    var Matrix = ajax.math.Matrix;

    this.graphicsDevice.transform.set_world(Matrix.identity());
    this.graphicsDevice.transform.set_view(
        Matrix.createLookAt(
            new ajax.math.Vector3(0, 0, 5),
            new ajax.math.Vector3(), 
            ajax.math.Vector3.up()
            )
    );
    this.graphicsDevice.transform.set_projection(
        Matrix.createPerspectiveFieldOfView(
            ajax.geometry.Math.piOver4, 
            this.graphicsDevice.viewport.width / this.graphicsDevice.viewport.height,
            0.1, 100
        )
    );

    cubeSample.Game1.superClass.initialize.call(this);
}
//-----------------------------------------------------------------------------

cubeSample.Game1.prototype.update = function(gameTime)
{
    gameTime = ajax.drawing.Timer.cast(gameTime);
    // TODO: Add your Update code here

    var rotSpeed = gameTime.elapsedTime * 0.001;
    this.graphicsDevice.transform.get_world().multiply(ajax.math.Matrix.createRotationY(rotSpeed));

    cubeSample.Game1.superClass.update.call(this, gameTime);
}
//-----------------------------------------------------------------------------

cubeSample.Game1.prototype.draw = function(gameTime)
{
    gameTime = ajax.drawing.Timer.cast(gameTime);

    this.graphicsDevice.clear(ajax.drawing.Colour.black());

    // TODO: Add your drawing code here
    this._mesh.draw(this.graphicsDevice);

    cubeSample.Game1.superClass.draw.call(this, gameTime);
}
//-----------------------------------------------------------------------------

cubeSample.Game1.prototype.endDraw = function()
{
    cubeSample.Game1.superClass.endDraw.call(this);
    this.get_window().setStatusText(this.gameTime.getFpsString() + " : " + this.graphicsDevice.getFaceInfo());
}

cubeSample.Game1.prototype.dispose = function()
{
    // TODO: Cleanup your NON GAME COMPONENT objects here (eg Sounds!). Note: All items in the Components array are cleaned for you.
    cubeSample.Game1.superClass.dispose.call(this);
}

//-----------------------------------------------------------------------------
// Always provide a static cast method to your classes
cubeSample.Game1.cast = function(obj)
{///<returns type="cubeSample.Game1"/>
    return obj;
}